<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="会议室名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入会议室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="calendarList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会议室名称" align="center" prop="roomName" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template v-slot="{ row }">
          <span v-if="row.status === 0">未开始</span>
          <span v-else-if="row.status === 1">进行中</span>
          <span v-else-if="row.status === 2">已结束</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetaile(scope.row)"
            v-hasPermi="['room:calendar:query']"
          >详情</el-button>
          <el-button
            v-if="scope.row.status == 2 && scope.row.feedbackStatus != 2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleFeedback(scope.row)"
            v-hasPermi="['room:calendar:edit']"
          >评价</el-button>
          <el-button
            v-if="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['room:calendar:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情日历对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期"
            :style="{ width: '200px' }">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="会议室名称" prop="roomName" >
          <el-input v-model="form.roomName" placeholder="请输入会议室名称" :style="{ width: '200px' }"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间"
            :style="{ width: '200px' }">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间"
            :style="{ width: '200px' }">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 评价与反馈对话框 -->
    <el-dialog :title="title" :visible.sync="openFeedbackDialog" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="反馈内容">
          <el-input v-model="form.content" placeholder="请输入反馈内容" type="textarea"/>
        </el-form-item>
        <el-form-item label="评分" prop="rating">
          <el-rate
            v-model="form.rating"
            show-text>
          </el-rate>
        </el-form-item>
        <el-form-item label="建议" prop="recommendation">
          <el-input v-model="form.recommendation" placeholder="请输入建议" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCalendar, getListCalendar, getCalendar, delCalendar, addCalendar, updateCalendar } from "@/api/room/calendar";
import { addFeedback } from "@/api/room/feedback";

export default {
  name: "Calendar",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日历表格数据
      calendarList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openFeedbackDialog: false, // 反馈对话框的可见性状态
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        date: null,
        roomId: null,
        reservationId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {

  },
  mounted(){
    this.getList();
  },
  methods: {
    /** 查询日历列表 */
    getList() {
      this.loading = true;
      getListCalendar(this.queryParams).then(response => {
        this.calendarList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openFeedbackDialog = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        calendarId: null,
        date: null,
        roomId: null,
        reservationId: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.calendarId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetaile(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "日历详情";
    },
    /** 评价按钮操作 */
    handleFeedback(row) {
      this.reset();
      this.openFeedbackDialog = true;
      this.form = row;
      this.title = "评价与反馈";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
          if (valid) {
          // 提交评价数据
          addFeedback(this.form).then(response => {
            this.$modal.msgSuccess("评价成功");
            this.openFeedbackDialog = false;
          });
          // 更新日历表的 feedbackStatus 值为2
          const params = {
            calendarId: this.form.calendarId,
            feedbackStatus: 2 // 将 feedbackStatus 设置为2
          };
          updateCalendar(params).then(() => {
            // 更新成功后重新获取列表数据
            this.getList();
          });
      }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const calendarIds = row.calendarId || this.ids;
      this.$modal.confirm('是否确认删除日历编号为"' + calendarIds + '"的数据项？').then(function() {
        return delCalendar(calendarIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('room/calendar/export', {
        ...this.queryParams
      }, `calendar_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
